﻿/* 
 *  <copyright file="BaseScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SobrietyEngine.Events;
using SobrietyEngine.Guids;
using SobrietyEngine.Input;

namespace SobrietyEngine.Screens
{
    public abstract class BaseScreen : IEventListener, SobrietyEngine.Screens.IScreen
    {
        public bool Initialized { get; set; }
        public bool Active { get; set; }
        public bool StealFocus { get; set; }
        public bool Popup { get; set; }
        public bool MarkedForRemoval { get; set; }
        public bool Visible { get; set; }
        public bool ListenersActive { get; set;}
        public bool Overlay { get; set; }
        
        public CGuid Owner { get; set; }
        public Viewport Viewport { get; set; }
        public Matrix ViewportTransformation { get; set; }

        public IScreenManager ScreenManager { get; set; }
        
        protected InputGameService inputGameService = null;
        protected IEventRouter EventRouter = null;      
        protected Sound.SoundGameService soundManager = null;

        public SpriteFont MenuSpriteFont { get; set;  }
       
        public BaseScreen()
        {
            Owner = null;
            ViewportTransformation = Matrix.Identity;
            Active = false;
            Initialized = false;
            StealFocus = false;
            Popup = false;
            MarkedForRemoval = false;
            Overlay = false;
        }

        public virtual void Initialize()
        {
            Initialized = true;
            Viewport = ScreenManager.FullScreenViewport;
            soundManager = (Sound.SoundGameService)ScreenManager.Game.Services.GetService(typeof(Sound.SoundGameService));
            EventRouter = (EventRouterGameService)ScreenManager.Game.Services.GetService(typeof(IEventRouter));
            inputGameService = (InputGameService)ScreenManager.Game.Services.GetService(typeof(InputGameService));
            MenuSpriteFont = ScreenManager.AssetGameService.Load<SpriteFont>("Fonts/menuFont");
            RegisterEventListeners();
        }

        public virtual void Show()
        {
            Active = true;
            Visible = true;
        }

        public virtual void Hide()
        {
            Active = false;
            Visible = false;
        }

        public virtual void Remove()
        {
            UnregisterEventListeners();
        }

        public abstract void UnregisterEventListeners();
        public abstract void RegisterEventListeners();

        public virtual void HandleEvent(Event e) { }

        public virtual void Update(GameTime time, Boolean coveredByOtherScreen, IScreen otherScreen)
        {
            if (coveredByOtherScreen)
                Hide();
            else
                Show();
        }

        public abstract void Draw(GameTime gameTime);
    }
}
